<template>
  <div>
    <el-select v-model="selectValue" placeholder="请选择" clearable @change="changeValue">
      <el-option v-for="item in allOption" :key="item.dictItemCode" :label="item.dictItemName"
        :value="item.dictItemCode" />
    </el-select>
  </div>
</template>
<script setup>
import { getDict } from '@/api/system/dict'
const props = defineProps({
  code: [String],
  value: [Number, String, Array]
})
const emits = defineEmits(['update'])
const selectValue = ref('')
const allOption = ref(null)
watch(() => props.value, async () => {
  if (props.code) {
    let res = await getDict(props.code)
    allOption.value = res.data
    if (allOption.value.length > 0) {
      selectValue.value = allOption.value.find(item => item.dictItemCode == props.value)?.dictItemName
    } else {
      selectValue.value = props.value
    }
  }
}, { deep: true, immediate: true })

const changeValue = (val) => {
  emits('update', val)
}
</script>